#include "utils.h"
#include <GL/gl.h>
#include <cstdlib>
#include <QDebug>

#include "tankmodel.h"

void Utils::renderMesh(int spacing, int width, int height)
{
    int numHorizontalLines = width / spacing;
    int numVerticalLines = height / spacing;

    glColor3f(.5, 1.0, .0);

    int halfWidth = width / 2;
    int halfHeight = height / 2;

    glBegin(GL_LINES);
        glVertex2i(0, halfHeight);
        glVertex2i(0, -halfHeight);
    glEnd();

    for(int i=0; i<numHorizontalLines; ++i) {
        int x = i * spacing;
        glBegin(GL_LINES);
            glVertex2i(x, halfHeight);
            glVertex2i(x, -halfHeight);
        glEnd();

        glBegin(GL_LINES);
            glVertex2i(-x, halfHeight);
            glVertex2i(-x, -halfHeight);
        glEnd();
    }

    glBegin(GL_LINES);
        glVertex2i(-halfWidth, 0);
        glVertex2i(halfWidth, 0);
    glEnd();

    for(int i=0; i<numVerticalLines; ++i) {
        int y = i * spacing;
        glBegin(GL_LINES);
            glVertex2i(-halfWidth, y);
            glVertex2i(halfWidth, y);
        glEnd();

        glBegin(GL_LINES);
            glVertex2i(-halfWidth, -y);
            glVertex2i(halfWidth, -y);
        glEnd();
    }
}

void Utils::designatePlayerPosition(TankModel &player, int sceneWidth)
{
    player.pos_ = (rand() % sceneWidth / 4) * 8;

    if(player.pos_ == 0) {
        player.pos_ += TankModel::width_ / 2.0;
    } else if(player.pos_ >= sceneWidth / 2) {
        player.pos_ = (sceneWidth / 2) - (TankModel::width_ / 2.0);
    }

    qDebug() << "Player pos = " << player.pos_;
}
